package com.alianzamedica.controllers.profile;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.objectsearch.web.tools.Converter;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Profile;
import com.alianzamedica.tools.Enviroment;

/**
 * @author Carlos
 * 
 */
public class ProfileEditAction extends Action {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		search.setDefaultDialect(ObjectSearch.MYSQL_DIALECT);
		Integer id = Converter.string2Integer(request.getParameter("id"));
		if (id != null) {
			Profile profile = new Profile();
			profile.setId(id);
			Iterator<Profile> iterator = search.searchObjects(profile)
					.iterator();
			if (iterator.hasNext()) {
				Profile profile2 = (Profile) iterator.next();
				request.setAttribute("profile", profile2);
			}
		}

		return mapping.findForward("success");
	}

}
